/**
 * 碰撞检测系统模块
 * 负责游戏中所有碰撞检测相关功能
 */

class CollisionSystem {
    
    // 通用碰撞检测
    static checkCollision(obj1, obj2) {
        const size1 = obj1.size || new Vector2D(8, 8);
        const size2 = obj2.size || new Vector2D(40, 40);
        
        return obj1.position.x < obj2.position.x + size2.x &&
               obj1.position.x + size1.x > obj2.position.x &&
               obj1.position.y < obj2.position.y + size2.y &&
               obj1.position.y + size1.y > obj2.position.y;
    }

    // 子弹与坦克碰撞检测（圆形碰撞）
    static checkBulletTankCollision(bullet, tank) {
        const bulletCenterX = bullet.position.x;
        const bulletCenterY = bullet.position.y;
        
        const tankCenterX = tank.position.x;
        const tankCenterY = tank.position.y;
        
        const dx = bulletCenterX - tankCenterX;
        const dy = bulletCenterY - tankCenterY;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        const tankRadius = 25;
        const bulletRadius = 4;
        
        return distance <= (tankRadius + bulletRadius);
    }

    // 子弹与建筑碰撞检测
    static checkBulletBuildingCollision(bullet, building) {
        const bulletCenterX = bullet.position.x;
        const bulletCenterY = bullet.position.y;
        const bulletRadius = 4;
        
        const buildingLeft = building.position.x;
        const buildingRight = building.position.x + building.size.x;
        const buildingTop = building.position.y;
        const buildingBottom = building.position.y + building.size.y;
        
        const closestX = Math.max(buildingLeft, Math.min(bulletCenterX, buildingRight));
        const closestY = Math.max(buildingTop, Math.min(bulletCenterY, buildingBottom));
        
        const dx = bulletCenterX - closestX;
        const dy = bulletCenterY - closestY;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        return distance <= bulletRadius;
    }

    // 坦克与建筑碰撞检测
    static checkTankBuildingCollision(tank, building) {
        const tankCenterX = tank.position.x;
        const tankCenterY = tank.position.y;
        const tankRadius = 22;
        
        const buildingLeft = building.position.x;
        const buildingRight = building.position.x + building.size.x;
        const buildingTop = building.position.y;
        const buildingBottom = building.position.y + building.size.y;
        
        const closestX = Math.max(buildingLeft, Math.min(tankCenterX, buildingRight));
        const closestY = Math.max(buildingTop, Math.min(tankCenterY, buildingBottom));
        
        const dx = tankCenterX - closestX;
        const dy = tankCenterY - closestY;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        return distance <= tankRadius;
    }

    // 坦克与坦克碰撞检测（圆形碰撞）
    static checkTankTankCollision(tank1, tank2) {
        const tank1CenterX = tank1.position.x;
        const tank1CenterY = tank1.position.y;
        const tank2CenterX = tank2.position.x;
        const tank2CenterY = tank2.position.y;
        
        const dx = tank1CenterX - tank2CenterX;
        const dy = tank1CenterY - tank2CenterY;
        const distance = Math.sqrt(dx * dx + dy * dy);
        
        const tank1Radius = 22; // 坦克半径
        const tank2Radius = 22;
        const minDistance = tank1Radius + tank2Radius;
        
        return distance <= minDistance;
    }

    // 检查视线是否被阻挡
    static checkLineOfSight(from, to, buildings) {
        if (!buildings || buildings.length === 0) return true;
        
        const direction = Vector2D.subtract(to, from);
        const distance = direction.magnitude();
        
        // 如果距离太近，直接返回true
        if (distance < 20) return true;
        
        // 使用更密集的检测点，提高精度
        const stepSize = 8; // 减少步长，增加检测精度
        const steps = Math.floor(distance / stepSize);
        const stepVector = direction.normalize().multiply(stepSize);
        
        for (let i = 1; i < steps; i++) {
            const checkPoint = Vector2D.add(from, Vector2D.multiply(stepVector, i));
            
            for (const building of buildings) {
                // 使用更精确的碰撞检测
                if (this.isPointInBuilding(checkPoint, building)) {
                    return false; // 视线被阻挡
                }
            }
        }
        
        return true; // 视线通畅
    }

    // 检查点是否在建筑物内
    static isPointInBuilding(point, building) {
        return point.x >= building.position.x && 
               point.x <= building.position.x + building.size.x &&
               point.y >= building.position.y && 
               point.y <= building.position.y + building.size.y;
    }

    // 检查位置是否超出边界
    static isOutOfBounds(position, margin = 50) {
        return position.x < -margin || position.x > window.innerWidth + margin ||
               position.y < -margin || position.y > window.innerHeight + margin;
    }

    // 将对象限制在屏幕内
    static clampToScreen(obj, margin = 20) {
        obj.position.x = Math.max(margin, Math.min(window.innerWidth - margin, obj.position.x));
        obj.position.y = Math.max(margin, Math.min(window.innerHeight - margin, obj.position.y));
    }
} 